package com.sannuo.detector.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sannuo.common.domain.*;
import com.sannuo.common.domain.vo.PatientAndStoreVo;
import com.sannuo.common.result.Result;
import com.sannuo.common.result.ResultCode;
import com.sannuo.detector.service.MedicalReportService;
import com.sannuo.detector.service.MedicalResultService;
import com.sannuo.detector.service.PatientService;
import com.sannuo.detector.service.ResultsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/detector")
@Api(tags = "检测者列表")
@CrossOrigin
public class DetectorController {

    @Autowired
    private PatientService patientService;
    @Autowired
    private MedicalResultService medicalResultService;
    @Autowired
    private ResultsService resultsService;
    @Autowired
    private MedicalReportService medicalReportService;

    @RequestMapping(value = "listPatient",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有的检测者信息",notes = "查询所有总店信息",httpMethod = "GET")
    public Result findAll(Integer page , Integer limit, String searchParams) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        PatientAndStoreVo patientAndStoreVo = null;
        if(searchParams != null){
            //将searchParams接收到的JSON字符串转换UserVo对象
            patientAndStoreVo = objectMapper.readValue(searchParams, PatientAndStoreVo.class);
        }
        Page page1 = PageHelper.startPage(page, limit);
        List<Patient> patientList = patientService.findAll(patientAndStoreVo);
        PageInfo pageInfo = new PageInfo<>(patientList);
        return Result.ok(ResultCode.SUCCESS, pageInfo.getTotal(), patientList);
    }

    @RequestMapping(value = "listResults/{patientId}",method = RequestMethod.GET)
    @ApiOperation(value = "查询检测者的检测结果",notes = "查询检测者的检测结果",httpMethod = "GET")
    public Result findAllResults(Integer page , Integer limit,@PathVariable("patientId") int id) {
        Page page1 = PageHelper.startPage(page, limit);
        List<Results> resultsList = resultsService.findAllResult(id);
        PageInfo pageInfo = new PageInfo<>(resultsList);
        return Result.ok(ResultCode.SUCCESS,pageInfo.getTotal(), resultsList);
    }

    @RequestMapping(value = "listResults2/{patientId}",method = RequestMethod.GET)
    @ApiOperation(value = "查询检测者的检测结果",notes = "查询检测者的检测结果",httpMethod = "GET")
    public Result findAllResults2(Integer page , Integer limit,@PathVariable("patientId") int id) {
        QueryWrapper<Results> queryWrapper = new QueryWrapper<>();
        Page page1 = PageHelper.startPage(page, limit);
        List<Results> resultsList = resultsService.list(queryWrapper.eq("result_id", id));
        PageInfo pageInfo = new PageInfo<>(resultsList);
        return Result.ok(ResultCode.SUCCESS,pageInfo.getTotal(), resultsList);
    }

    @RequestMapping(value = "listMedicalResult/{patientId}",method = RequestMethod.GET)
    @ApiOperation(value = "查询检测者的检测的记录",notes = "查询检测者的检测的记录",httpMethod = "GET")
    public Result findAllMedicalResult(@PathVariable("patientId") int id) {
        QueryWrapper<MedicalResult> queryWrapper = new QueryWrapper<>();
        List<MedicalResult> medicalResultList = medicalResultService.list(queryWrapper.eq("patient_id", id));
        return Result.ok(ResultCode.SUCCESS, medicalResultList);
    }

    @RequestMapping(value = "findBranchStore",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有的分店" , notes = "查询所有的分店" ,httpMethod = "GET")
    public Result findBranchStore() {
        List<DrugStore> drugStoreList = patientService.findAllBranchStore();
        return Result.ok(ResultCode.SUCCESS,drugStoreList);
    }

    @RequestMapping(value = "addPatient/{userId}",method = RequestMethod.POST)
    @ApiOperation(value = "新增检测者" , notes = "新增检测者" ,httpMethod = "POST")
    public Result addPatient(Patient patient,@PathVariable("userId") Integer userId) {
        System.out.println("patientId:"+patient.getPatientId());
        Random random = new Random();
        int code1 = random.nextInt(1000000) + 1;
        int code2 = random.nextInt(10000) + 1;
        String customerCode = "ENF" + code1;
        String patientCode = "NO" + code2;
//        System.out.println(patient.getPatientId());
        patient.setCustomerCode(customerCode);
        patient.setPatientCode(patientCode);
        patient.setUserId(userId);
        int row = patientService.addPatient(patient);
        if (row > 0) {
            return Result.ok(ResultCode.SUCCESS,row);
        } else {
            return Result.fail(ResultCode.FAIL,row);
        }
    }

    @RequestMapping(value = "addPatient2",method = RequestMethod.POST)
    @ApiOperation(value = "完善检测者的信息" , notes = "完善检测者的信息" ,httpMethod = "POST")
    public Result addPatient2(Patient patient, MedicalResult medicalResult, Results results) {
//        System.out.println("ppppppppppppppppp"+patient);
        if(patient.getHeight()!=null || patient.getWeight()!=null) {
            medicalResult.setBmiResult(patient.getWeight()/((patient.getHeight()*0.01)*(patient.getHeight()*0.01)));
        } else {
            medicalResult.setBmiResult(0.0);
        }
        List<Patient> patientList = patientService.list();
        for (Patient patient1 : patientList) {
            if (patient1.getPatientId()==patient.getPatientId()) {
                if (patient1.getCountOfTest()==null) {
                    patient.setCountOfTest(1);
                } else {
                    patient.setCountOfTest(patient1.getCountOfTest()+1);
                }
            }
        }
        patientService.updatePatient(patient);
        medicalResultService.addMedicalResult(medicalResult);
//        System.out.println("mmmmmmmmmmmmmmmmm"+medicalResult);
        List<MedicalReport> medicalReportList = medicalReportService.list();
        results.setResultId(medicalResult.getResultId());
//        System.out.println("medicalResult++++++++++++++"+medicalResult);
        if (medicalResult.getTgResult()!=null) {
            results.setMedicalName(medicalReportList.get(0).getMedicalName());
            results.setMedicalCode(medicalReportList.get(0).getMedicalCode());
            results.setMedicalMethod(medicalReportList.get(0).getMedicalMethod());
            results.setMedicalUnits(medicalReportList.get(0).getMedicalUnits());
            results.setMedicalReference(medicalReportList.get(0).getMedicalReference());
            if (medicalResult.getTgResult() < medicalReportList.get(0).getReferenceLow()) {
                results.setMedicalSuggest("偏低注意生活习惯、饮食规律");
            } else if (medicalResult.getTgResult() > medicalReportList.get(0).getReferenceHigh()) {
                results.setMedicalSuggest("偏高注意生活习惯、饮食规律");
            } else {
                results.setMedicalSuggest("正常");
            }
            results.setMedicalResult(medicalResult.getTgResult().toString());
            resultsService.addReportList(results);
        }
        if (medicalResult.getGluResult()!=null) {
            results.setMedicalName(medicalReportList.get(1).getMedicalName());
            results.setMedicalCode(medicalReportList.get(1).getMedicalCode());
            results.setMedicalMethod(medicalReportList.get(1).getMedicalMethod());
            results.setMedicalUnits(medicalReportList.get(1).getMedicalUnits());
            results.setMedicalReference(medicalReportList.get(1).getMedicalReference());
            if (medicalResult.getGluResult() < medicalReportList.get(1).getReferenceLow()) {
                results.setMedicalSuggest("偏低注意生活习惯、饮食规律");
            } else if (medicalResult.getGluResult() > medicalReportList.get(1).getReferenceHigh()) {
                results.setMedicalSuggest("偏高注意生活习惯、饮食规律");
            } else {
                results.setMedicalSuggest("正常");
            }
            results.setMedicalResult(medicalResult.getGluResult().toString());
            resultsService.addReportList(results);
        }
        if (medicalResult.getHbaicResult()!=null) {
            results.setMedicalName(medicalReportList.get(2).getMedicalName());
            results.setMedicalCode(medicalReportList.get(2).getMedicalCode());
            results.setMedicalMethod(medicalReportList.get(2).getMedicalMethod());
            results.setMedicalUnits(medicalReportList.get(2).getMedicalUnits());
            results.setMedicalReference(medicalReportList.get(2).getMedicalReference());
            if (medicalResult.getHbaicResult() < medicalReportList.get(2).getReferenceLow()) {
                results.setMedicalSuggest("偏低注意生活习惯、饮食规律");
            } else if (medicalResult.getHbaicResult() > medicalReportList.get(2).getReferenceHigh()) {
                results.setMedicalSuggest("偏高注意生活习惯、饮食规律");
            } else {
                results.setMedicalSuggest("正常");
            }
            results.setMedicalResult(medicalResult.getHbaicResult().toString());
            resultsService.addReportList(results);
        }
        if (medicalResult.getUaResult()!=null) {
            results.setMedicalName(medicalReportList.get(3).getMedicalName());
            results.setMedicalCode(medicalReportList.get(3).getMedicalCode());
            results.setMedicalMethod(medicalReportList.get(3).getMedicalMethod());
            results.setMedicalUnits(medicalReportList.get(3).getMedicalUnits());
            results.setMedicalReference(medicalReportList.get(3).getMedicalReference());
            if (medicalResult.getUaResult() < medicalReportList.get(3).getReferenceLow()) {
                results.setMedicalSuggest("偏低注意生活习惯、饮食规律");
            } else if (medicalResult.getUaResult() > medicalReportList.get(3).getReferenceHigh()) {
                results.setMedicalSuggest("偏高注意生活习惯、饮食规律");
            } else {
                results.setMedicalSuggest("正常");
            }
            results.setMedicalResult(medicalResult.getUaResult().toString());
            resultsService.addReportList(results);
        }
        if (medicalResult.getHdlCResult()!=null) {
            results.setMedicalName(medicalReportList.get(4).getMedicalName());
            results.setMedicalCode(medicalReportList.get(4).getMedicalCode());
            results.setMedicalMethod(medicalReportList.get(4).getMedicalMethod());
            results.setMedicalUnits(medicalReportList.get(4).getMedicalUnits());
            results.setMedicalReference(medicalReportList.get(4).getMedicalReference());
            if (medicalResult.getHdlCResult() < medicalReportList.get(4).getReferenceLow()) {
                results.setMedicalSuggest("偏低注意生活习惯、饮食规律");
            } else if (medicalResult.getHdlCResult() > medicalReportList.get(4).getReferenceHigh()) {
                results.setMedicalSuggest("偏高注意生活习惯、饮食规律");
            } else {
                results.setMedicalSuggest("正常");
            }
            results.setMedicalResult(medicalResult.getHdlCResult().toString());
            resultsService.addReportList(results);
        }
        if (medicalResult.getLdlCResult()!=null) {
            results.setMedicalName(medicalReportList.get(5).getMedicalName());
            results.setMedicalCode(medicalReportList.get(5).getMedicalCode());
            results.setMedicalMethod(medicalReportList.get(5).getMedicalMethod());
            results.setMedicalUnits(medicalReportList.get(5).getMedicalUnits());
            results.setMedicalReference(medicalReportList.get(5).getMedicalReference());
            if (medicalResult.getLdlCResult() < medicalReportList.get(5).getReferenceLow()) {
                results.setMedicalSuggest("偏低注意生活习惯、饮食规律");
            } else if (medicalResult.getLdlCResult() > medicalReportList.get(5).getReferenceHigh()) {
                results.setMedicalSuggest("偏高注意生活习惯、饮食规律");
            } else {
                results.setMedicalSuggest("正常");
            }
            results.setMedicalResult(medicalResult.getLdlCResult().toString());
            resultsService.addReportList(results);
        }
        if (medicalResult.getCholResult()!=null) {
            results.setMedicalName(medicalReportList.get(6).getMedicalName());
            results.setMedicalCode(medicalReportList.get(6).getMedicalCode());
            results.setMedicalMethod(medicalReportList.get(6).getMedicalMethod());
            results.setMedicalUnits(medicalReportList.get(6).getMedicalUnits());
            results.setMedicalReference(medicalReportList.get(6).getMedicalReference());
            if (medicalResult.getCholResult() < medicalReportList.get(6).getReferenceLow()) {
                results.setMedicalSuggest("偏低注意生活习惯、饮食规律");
            } else if (medicalResult.getCholResult() > medicalReportList.get(6).getReferenceHigh()) {
                results.setMedicalSuggest("偏高注意生活习惯、饮食规律");
            } else {
                results.setMedicalSuggest("正常");
            }
            results.setMedicalResult(medicalResult.getCholResult().toString());
            resultsService.addReportList(results);
        }
        if (medicalResult.getKetResult()!=null) {
            results.setMedicalName(medicalReportList.get(7).getMedicalName());
            results.setMedicalCode(medicalReportList.get(7).getMedicalCode());
            results.setMedicalMethod(medicalReportList.get(7).getMedicalMethod());
            results.setMedicalUnits(medicalReportList.get(7).getMedicalUnits());
            results.setMedicalReference(medicalReportList.get(7).getMedicalReference());
            if (medicalResult.getKetResult() < medicalReportList.get(7).getReferenceLow()) {
                results.setMedicalSuggest("偏低注意生活习惯、饮食规律");
            } else if (medicalResult.getKetResult() > medicalReportList.get(7).getReferenceHigh()) {
                results.setMedicalSuggest("偏高注意生活习惯、饮食规律");
            } else {
                results.setMedicalSuggest("正常");
            }
            results.setMedicalResult(medicalResult.getKetResult().toString());
            resultsService.addReportList(results);
        }
        if (medicalResult.getSbpResult()!=null) {
            results.setMedicalName(medicalReportList.get(8).getMedicalName());
            results.setMedicalCode(medicalReportList.get(8).getMedicalCode());
            results.setMedicalMethod(medicalReportList.get(8).getMedicalMethod());
            results.setMedicalUnits(medicalReportList.get(8).getMedicalUnits());
            results.setMedicalReference(medicalReportList.get(8).getMedicalReference());
            if (medicalResult.getSbpResult() < medicalReportList.get(8).getReferenceLow()) {
                results.setMedicalSuggest("偏低注意生活习惯、饮食规律");
            } else if (medicalResult.getSbpResult() > medicalReportList.get(8).getReferenceHigh()) {
                results.setMedicalSuggest("偏高注意生活习惯、饮食规律");
            } else {
                results.setMedicalSuggest("正常");
            }
            results.setMedicalResult(medicalResult.getSbpResult().toString());
            resultsService.addReportList(results);
        }
        if (medicalResult.getDbpResult()!=null) {
            results.setMedicalName(medicalReportList.get(9).getMedicalName());
            results.setMedicalCode(medicalReportList.get(9).getMedicalCode());
            results.setMedicalMethod(medicalReportList.get(9).getMedicalMethod());
            results.setMedicalUnits(medicalReportList.get(9).getMedicalUnits());
            results.setMedicalReference(medicalReportList.get(9).getMedicalReference());
            if (medicalResult.getDbpResult() < medicalReportList.get(9).getReferenceLow()) {
                results.setMedicalSuggest("偏低注意生活习惯、饮食规律");
            } else if (medicalResult.getDbpResult() > medicalReportList.get(9).getReferenceHigh()) {
                results.setMedicalSuggest("偏高注意生活习惯、饮食规律");
            } else {
                results.setMedicalSuggest("正常");
            }
            results.setMedicalResult(medicalResult.getDbpResult().toString());
            resultsService.addReportList(results);
        }
        int row = patientService.updatePatient2(patient.getPatientId());
        if (row > 0) {
            return Result.ok(ResultCode.SUCCESS,row);
        } else {
            return Result.fail(ResultCode.FAIL,row);
        }
    }

    @RequestMapping(value = "updatePatient",method = RequestMethod.POST)
    @ApiOperation(value = "修改检测者的信息" , notes = "修改检测者的信息" ,httpMethod = "POST")
    public Result updatePatient(Patient patient) {
//        System.out.println("ididididididididiiddiididi=======》"+patient.getPatientId());
        int row = patientService.updatePatient(patient);
        if (row > 0) {
            return Result.ok(ResultCode.SUCCESS,row);
        } else {
            return Result.fail(ResultCode.FAIL,row);
        }
    }

    @RequestMapping(value = "updatePatient2",method = RequestMethod.POST)
    @ApiOperation(value = "修改检测者的信息" , notes = "修改检测者的信息" ,httpMethod = "POST")
    public Result updatePatient2(Patient patient) {
        patient.setStatus(0);
        System.out.println(patient);
        int row = patientService.updatePatient(patient);
        if (row > 0) {
            return Result.ok(ResultCode.SUCCESS,row);
        } else {
            return Result.fail(ResultCode.FAIL,row);
        }
    }
}
